package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.config.MyUserConfig;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Role;
import com.itheima.pojo.User;
import com.itheima.service.UserService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 吴
 * @version V1.0
 * @Description
 * @Package com.itheima.controller
 * @date 2021/11/28 17:27
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Reference
    private UserService userService;

    /**
     * 获取用户信息
     * @return
     */
    @GetMapping("/getUsername")
    public Result getUsername(){
        try {
            MyUserConfig principal = (MyUserConfig)SecurityContextHolder.getContext().getAuthentication().getPrincipal();
            return new Result(true, MessageConstant.GET_USERNAME_SUCCESS,principal.getUser());
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.GET_USERNAME_FAIL);
        }
    }

        /**
            * @Description: 分页查询
            * @Param:
            * @return:
            * @Author: 文攀
            * @Date: 2021/12/8
            */
    @GetMapping("/findPage")
    public Result findPage(QueryPageBean queryPageBean){
        try {
            PageResult result=userService.selectByCondition(queryPageBean);
            return new Result(true,"查询成功",result);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"查询失败");
        }



    }

        /**
            * @Description:  查询所有角色
            * @Param:
            * @return:
            * @Author: 文攀
            * @Date: 2021/12/8
            */
    @GetMapping("/findRoles")
    public Result findRoles(){
        try {
            List<Role> roles= userService.findRoles();
            return new Result(true,"查询成功",roles);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"查询失败");
        }
    }
    /**
        * @Description: 添加用户
        * @Param: user，roles
        * @return:
        * @Author: 文攀
        * @Date: 2021/12/8
        */
    @PostMapping("/add")
    public Result add(@RequestBody User user,@RequestParam("roles") Integer[] roles){
        try {
            userService.add(user,roles);
            return new Result(true,"添加用户成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"添加用户失败");
        }

    }

        /**
            * @Description: 根据用户id查询用户信息
            * @Param: id（用户id）
            * @return:
            * @Author: 文攀
            * @Date: 2021/12/8
            */
    @GetMapping("/findById/{id}")
    public Result findById(@PathVariable Integer id){
        try {
            User user= userService.findById(id);
            return new Result(true,"查询成功",user);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"查询失败");
        }

    }

        /**
            * @Description: 根据用户id查询用户所有的角色
            * @Param:
            * @return:
            * @Author: 文攀
            * @Date: 2021/12/8
            */
        @GetMapping("/findRolesByUserId/{userId}")
    public Result findRolesByUserId(@PathVariable Integer userId){
            try {
               Integer[] roleIds=userService.findRolesByUserId(userId);
                return new Result(true,"查询成功",roleIds);
            } catch (Exception e) {
                e.printStackTrace();
                return new Result(false,"查询失败");
            }

    }

        /**
            * @Description:  修改用户
            * @Param:
            * @return:
            * @Author: 文攀
            * @Date: 2021/12/8
            */
    @PutMapping("/edit")
    public Result edit(@RequestBody User user,@RequestParam("roleIds") Integer[] roleIds){
        try {
            userService.update(user,roleIds);
            return new Result(true,"修改成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"修改失败");
        }

    }

        /**
            * @Description: 根据用户id删除用户
            * @Param: userId(用户id)
            * @return:
            * @Author: 文攀
            * @Date: 2021/12/9
            */
    @DeleteMapping("/{userId}")
    public Result deleteById(@PathVariable("userId") Integer userId){
        try {
            userService.deleteById(userId);
            return new Result(true,"删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"删除失败");
        }
    }

    /**
     * 查找所有体质项（中医体质辨识）
     * @param queryPageBean
     * @return
     */
    @GetMapping("/findPhysical")
    public Result findPhysical(QueryPageBean queryPageBean){
        try {
            PageResult result=userService.findPhysical(queryPageBean);
            return new Result(true,"查询成功",result);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(true,"查询成功");
        }

    }
}

